<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">产业地图后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else style="color:#ffffff;">登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
      <div class="el-login-footer">
        <span>© 2021-2025 湖北云顶数字科技有限责任公司 版权所有</span>
      </div>
    </el-form>

  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: 'super',
        password: '123456',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.data.img
          this.loginForm.uuid = res.data.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' }).catch(() => {
            })
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100vh;

  /* 背景图 */
  background: url('~@/assets/images/bg1.jpg') no-repeat center center;
  background-size: 100% 100%; /* 等比例铺满屏幕 */
  padding-right: 8%;
}

.login-form {
  width: 420px;
  padding: 42px 38px 32px;
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(18px);
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  animation: fadeIn 0.6s ease;

  /* 标题 */
  .title {
    text-align: center;
    margin-bottom: 38px;
    color: #d6d6d6;
    font-weight: 600;
    font-size: 20px;
    letter-spacing: 0.5px;
  }

  /* 输入框只作用此页面 */
  .el-input input {
    background: rgba(255, 255, 255, 0.08) !important;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    border-radius: 8px !important;
    color: #e5e7eb !important;
    transition: border-color .25s, box-shadow .25s;
  }

  .el-input input:focus {
    border-color: #6520d3 !important;
  }

  /* 验证码输入与图片水平排列 */
  .el-form-item[prop="code"] {
    display: flex;
    align-items: center;
  }

  .el-form-item[prop="code"] .el-input {
    flex: 1;
    margin-right: 10px;
  }

  .login-code {
    margin-top: 10px;
  }

  .login-code-img {
    height: 38px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    cursor: pointer;
    opacity: 0.9;
    transition: .3s;
  }

  .login-code-img:hover {
    opacity: 1;
    transform: scale(1.03);
  }

  /* 登录按钮范围内生效 */
  .el-button--primary {
    border-radius: 8px;
    height: 42px;
    font-weight: 500;
    letter-spacing: 1px;
    background: #6520d3;
    border: none;
    transition: .3s;
  }

  .el-button--primary:hover {
    background: #471695;
    box-shadow: 0 6px 20px rgba(37, 99, 235, 0.45);
    transform: translateY(-1px);
  }
  .el-button--primary.is-loading {
    background-color: #471695 !important;
    border-color: #471695 !important;
    opacity: 1 !important;
    color: #fff !important;
  }
  /* 记住密码文字 */
  .el-checkbox__label {
    color: #3c3c3c !important;
  }
}

.el-login-footer {
  position: fixed;
  bottom: 25px;
  width: 100%;
  text-align: center;
  color: #000000;
  font-size: 12px;
  letter-spacing: 0.5px;
  padding-right: 15%;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>


